import {PlusOutlined} from '@ant-design/icons';
import {Button, Divider, Popconfirm, Select} from 'antd';
import React, { useState, useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';

import { page,remove } from './service';
import CreatingSteps from "./CreatingSteps";


const TableList = () => {
  const [create, handleCreate] = useState(false);
  const [stepFormValues, setStepFormValues] = useState({});
  const [revise, setReviseState] = useState(false);
  const actionRef = useRef();
  const [count, setCount] = useState();

  const columns = [
    {
      title: '赠品名称',
      dataIndex: 'name',
      formItemProps: {
        rules: [
          {
            required: true,
            message: '赠品名称为必填项',
          },
        ],
      },
    },
    {
      title: '赠品条码',
      dataIndex: 'barCode',
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => {
        return (
          <>
            <a
              onClick={() => {
                setStepFormValues(record);
                handleCreate(true);
                setReviseState(true);
              }}
            >
              修改
            </a>
            <Divider type="vertical" />
            <Popconfirm
              placement="top"
              title="确定要删除吗?"
              onConfirm={() => {
                remove(record.id).then(resp=>{
                  actionRef.current.reload();
                });
              }}
              okText="确定"
              cancelText="取消"
            >
              <a>删除</a>
            </Popconfirm>
          </>
        );
      },
    },
  ];
  return (
    <PageContainer className="pageContainer">
      {create ? (
        <CreatingSteps
          backTable={() => {
            handleCreate(false);
            setStepFormValues({});
          }}
          nowStep={stepFormValues}
          state={revise}
        />
      ) : (
        <ProTable
          headerTitle="赠品列表"
          actionRef={actionRef}
          rowKey="id"
          scroll={{ x: 1200 }}
          bordered
          search={{
            labelWidth: 120,
          }}
          toolBarRender={() => [
            <Button
              type="primary"
              onClick={() => {
                handleCreate(true);
                setReviseState(false);
                // handleModalVisible(true);
              }}
            >
              <PlusOutlined /> 新建赠品
            </Button>,
          ]}
          request={(params, sorter, filter) => {
            return page({ ...params, sorter, filter });
          }}
          columns={columns}
          postData={(payload) => {
            const { list, total } = payload;
            setCount(total);
            return list||[];
          }}
          pagination={{
            defaultPageSize: 30,
            showSizeChanger: false,
            showTitle: false,
            total: count,
            showTotal: (total) => `共${total}条`,
          }}
        />
      )}
    </PageContainer>
  );
};

export default TableList;
